package xyz.cfg.spring

import org.springframework.util.PropertiesPersister
import scala.beans.BeanProperty
import java.io.{ Writer, InputStreamReader, IOException, OutputStream, InputStream, Reader }
import xyz.cfg.CfgFactory
import xyz.cfg.Cfg
import java.util.Properties
import java.io.IOException

class XyzCfgPropertiesPersister extends PropertiesPersister {
  @BeanProperty var splists: List[String] = {
    var res = List[String]()
    res.+:("_")
    res.+:("-")
    res
  }

  @throws(classOf[IOException])
  override def load(props: Properties, is: InputStream): Unit = {
    val reader: Reader = new InputStreamReader(is,"UTF-8")
    load(props, reader)
  }

  @throws(classOf[IOException])
  override def load(props: Properties, reader: Reader): Unit = {
    val conf: Cfg = CfgFactory.parseReader(reader)
    val set: Map[String, String] = conf.entry
    for (key <- set.keys) {
      val valueString: String =conf.getString(key)
      props.put(key, valueString)
      for (s <- splists) {
        val key2 = key.replaceAll("\\.", s)
        props.put(key2, valueString)
      }
    }
  }

  @throws(classOf[IOException])
  override def store(props: Properties, os: OutputStream, header: String): Unit = {

  }

  @throws(classOf[IOException])
  override def store(props: Properties, writer: Writer, header: String): Unit = {

  }
  @throws(classOf[IOException])
  override def loadFromXml(props: Properties, is: InputStream): Unit = {

  }

  @throws(classOf[IOException])
  override def storeToXml(props: Properties, os: OutputStream, header: String): Unit = {

  }

  @throws(classOf[IOException])
  override def storeToXml(props: Properties, os: OutputStream, header: String, encoding: String): Unit = {

  }
}